/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-02-23 10:15:33
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-08-02 15:05:27
 * @FilePath: \cxl-h5\src\pages\login\retrieve\index.ts
 */
import { resetPassword } from '@/apis/login'
import { CxlMessage } from '@/components/message'
import { Ref, ref } from 'vue'
import { RegisterBaseInformationData } from '../base/index'

export class RetrieveData extends RegisterBaseInformationData {
    active: Ref<boolean>
    isPending = false
    constructor() {
        super()
        this.active = ref(false)
    }
    onBaseInformationUpdate = (res: any) => {
        this.phone.value = res.phone
        this.code.value = res.code
        this.password.value = res.password
        this.passwordAgain.value = res.passwordAgain

        if (res.phone.length && res.code.length && res.password.length && res.passwordAgain.length) {
            this.active.value = true
        } else {
            this.active.value = false
        }
    }
    onRetrieveConfirm = (baseRef: any) => {
        if (this.isPending) {
            return
        }

        if (baseRef.validate()) {
            this.isPending = true
            resetPassword(this.code.value, this.password.value, this.phone.value).then((res: any) => {
                this.isPending = false
                if (res.data.value === 200) {
                    CxlMessage.success('您的密码已经更改成功！')
                    this.router.replace('/login')
                } else if (res.data.value === 20002) {
                    CxlMessage.error('验证码过期，请重新获取！')
                }
            })
        }
    }
}
